<?php
namespace app\beautiful\helper\payment;
use app\beautiful\logic\order\OrderLogic;
use app\beautiful\model\project\OnceCardModel;
use app\common\helper\payment\PaymentBaseHelper;
use app\beautiful\model\order\BalanceOrderModel;
use app\beautiful\logic\LogsLogic;
use app\beautiful\model\CouponModel;
use app\beautiful\model\member\MemberCouponModel;
use app\beautiful\logic\MemberWalletLogic;
use app\common\model\ShopGmvLogsModel;

//每个子类只需要专注于自己的 respond 逻辑
class PaymentBalanceHelper extends PaymentBaseHelper{
    protected  $moudel = 'beautiful';
    protected  $orderKey = 'mm'; //给一个KEY增大吞吐量
    protected  $respondAction = 'balanceRespond';
    
    public function respondCallback($orderId,$payInfo,$fail){
        $order = BalanceOrderModel::find($orderId);
        if(null === $order) return;
        if($order->is_paid == 0){ 
                $order->is_paid = 1;
                $order->payinfo = $payInfo ;
                $order->pay_time = time();
                $order->save();
                $memberId = $order->member_id;
                $shopId   = $order->shop_id;
                $recharge_price = $order->recharge_price;
                $give_price     = $order->give_price;
                $MemberWalletLogic = new MemberWalletLogic($shopId,$memberId);
                $MemberWalletLogic->addBalance($recharge_price, $give_price, 1);
                
                
                if(!empty($order->coupons)){
                    $coupons = CouponModel::where([
                        ['coupon_id','IN',$order->coupons]
                    ])->select()->toArray();
                    foreach($coupons as $val){ //写入优惠券
                        $memberCoupon = new MemberCouponModel();
                        $memberCoupon->save([
                            'shop_id'   => $shopId,
                            'member_id' => $memberId,
                            'type_id'   => 2,
                            'coupon_id' =>  $val['coupon_id'],
                            'name'      => $val['name'],
                            'coupon_money'  => $val['coupon_money'],
                            'money_limit'   => $val['money_limit'],
                            'valid_bg_time' => $val['valid_bg_time'],
                            'valid_end_time' => $val['valid_end_time'],
                            'status'    => 0,
                        ]);
                        LogsLogic::coupon($shopId, $memberId, 1, 11, $memberCoupon->member_coupon_id, '',0);
                    }
                }

                if(!empty($order->once_cards)){

//                    $once_cards = OnceCardModel::where([
//                        ['once_card_id','IN',$order->once_cards]
//                    ])->field('once_card_id')->select()->toArray();

                    $order_logic = OrderLogic::init($order->shop_id);
                    foreach ($order->once_cards as  $once_card_id){
                        $order_logic['once_card']->sendOnceCard([
                            'member_id'=>$order->member_id,
                            'once_card_id'=>$once_card_id,
                        ]);
                        wyuLog('error.text','0');
                    }

;                }


                //这里不需要记录分销业绩，因为在余额新增那边处理了
                
                //插入日志
                $shop_gmv_logs = new ShopGmvLogsModel();
                $shop_gmv_logs->shop_id=$order->shop_id;
                $shop_gmv_logs->balance=$recharge_price;
                $shop_gmv_logs->save();
                return true;
        }
        
    }
    
}